/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package docview;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Dema
 */
class BindTable extends JTable
{
    @Override
    public void changeSelection(int rowIndex, int columnIndex,
            boolean toggle, boolean extend) {
        super.changeSelection(rowIndex, columnIndex, true, false);
    }
}


public class BindWindow extends JPanel implements ActionListener,IObserver {
    protected BindTable table;
    protected JScrollPane scroller;
    protected DefaultTableModel tableModel;
    protected JButton bAdd;
    protected JButton bDelete;
    protected JButton bCategory;
    private Vector<IObserver> observers = new Vector();
    private int firstColCount = 0;
    private int secondColCount = 0;

    BindWindow()
    {
        initComponent();
    }

    private void initComponent()
    {
        tableModel = new DefaultTableModel(0,2);
        table = new BindTable();
        table.setModel(tableModel);
        bAdd = new JButton("Add");
        bDelete = new JButton("Delete");
        bCategory = new JButton("Edit");
        bAdd.addActionListener(this);
        bCategory.addActionListener(this);
        bDelete.addActionListener(this);
        scroller = new javax.swing.JScrollPane(table);
        BorderLayout layout = new BorderLayout();
        setLayout(layout);
        add(scroller, BorderLayout.CENTER);
        Box box = Box.createHorizontalBox();
        box.add(bAdd);
        box.add(bCategory);
        box.add(bDelete);
        add(box,BorderLayout.SOUTH);
        ListSelectionModel cellSelectionModel = table.getSelectionModel();
        cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent e) {

                    int i = table.getSelectedRow();
                    
                    if (i == -1)
                        highlightOff();
                    else
                    {
                        //table.clearSelection();
                        //table.addRowSelectionInterval(i, i);
                        //table.addColumnSelectionInterval(0, 1);
                        highlightOn(i);
                    }
                }

        });
        //setSize(50,250);
    }

    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == bAdd)
        {
            int i;
            i = table.getSelectedRow();
            if (i == -1)
                i = table.getRowCount();
            this.add(i);
        }
        if (source == bCategory)
        {

        }
        if (source == bDelete)
        {
            int i = table.getSelectedRow();
            delete(i);
            tableModel.removeRow(i);

        }

    }

    public void add(int i) {
        Iterator<IObserver> it = observers.iterator();
        while(it.hasNext())
        {
            it.next().add(i);
        }
    }

    public void highlightOn(int i) {
        Iterator<IObserver> it = observers.iterator();
        while(it.hasNext())
        {
            it.next().highlightOn(i);
        }
    }

    public void highlightOff() {
        Iterator<IObserver> it = observers.iterator();
        while(it.hasNext())
        {
            it.next().highlightOff();
        }
    }

    public void highlight(Selection one, Sender _sender) {
//
    }

    public void delete(int i) {
        Iterator<IObserver> it = observers.iterator();
        while(it.hasNext())
        {
            it.next().delete(i);
        }
    }

    public void createSelection(Selection selection, Sender sender,int i) {

        if (tableModel.getRowCount() <= i)
        {
              tableModel.addRow(new Vector(2));
        }
        if (sender == Sender.PRICE)
        {   
            tableModel.setValueAt(String.format("%d %d", selection.getX(),selection.getY()), i, 0);
        }
        else
        {
            tableModel.setValueAt(String.format("%d %d", selection.getX(),selection.getY()), i, 1);
        }
    }

    public void addListener(IObserver obj) {
        observers.add(obj);
    }

    public void deleteListener(IObserver obj) {
        Iterator<IObserver> it = observers.iterator();
        while(it.hasNext())
        {
            if(it.next().equals(obj))
            {
                it.remove();
                break;
            }

        }
    }



}
